package com.ozy.kxreader.mvp.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.ozy.kxreader.R
import com.ozy.kxreader.base.BaseActivity
import com.ozy.kxreader.di.component.AppComponent
import com.ozy.kxreader.di.component.DaggerBookListComponent
import com.ozy.kxreader.di.module.BookListModule
import com.ozy.kxreader.mvp.contract.BookListContract
import com.ozy.kxreader.mvp.model.entity.BookList
import com.ozy.kxreader.mvp.presenter.BookListPresenter
import com.ozy.kxreader.mvp.ui.adapter.BookListAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener
import kotlinx.android.synthetic.main.activity_booklist.*

/**
 * Created by 欧源 on 2018/2/2.
 */
class BookListActivity : BaseActivity<BookListPresenter>(), BookListContract.View, OnLoadmoreListener, OnRefreshLoadmoreListener {

    private val mTypes = arrayOf("hot", "new", "reputation", "over")

    private var mCategoryName: String? = null

    private var mType: String? = null

    private var mGender: String? = null

    private var mMinor: String? = null

    private var mBookListAdapter: BookListAdapter? = null

    override fun layoutId(): Int = R.layout.activity_booklist

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerBookListComponent
                .builder()
                .appComponent(appComponent)
                .bookListModule(BookListModule(this))
                .build()
                .inject(this)
    }

    companion object {
        val CATEGORY_NAME = "categoryName"
        val GENDER = "gender"

        fun start(context: Context, categoryName: String, gender: String) {
            val intent = Intent(context, BookListActivity::class.java)
            intent.putExtra(CATEGORY_NAME, categoryName)
            intent.putExtra(GENDER, gender)
            context.startActivity(intent)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        mCategoryName = intent.getStringExtra(CATEGORY_NAME);
        mGender = intent.getStringExtra(GENDER)
        mType = mTypes[0]
        initRecyclerView();
        initRefreshLayout()
        refreshLayout.autoRefresh()
    }

    private fun initRefreshLayout() {
        refreshLayout.setOnRefreshLoadmoreListener(this)
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        refresh(false)
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        refresh(true)
    }

    private fun refresh(isRefresh: Boolean) {
        mPresenter.getBookList(mGender, mCategoryName,
                if ("全部".equals(mMinor)) null else mMinor, mType, isRefresh)
    }


    private fun initRecyclerView() {
        mBookListAdapter = BookListAdapter()
        recyclerView.adapter = mBookListAdapter
    }


    override fun showBookList(books: List<BookList.BooksBean>, refresh: Boolean) {
        if (refresh)
            mBookListAdapter?.setNewData(books)
        else
            mBookListAdapter?.addData(books)
    }
}
